/*
	Copyright (C) 2009 jungil Han <jungil.Han@gmail.com>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mkButton.h"

#include <QPainter>

mkButton::mkButton( IN QWidget* a_pParent ) : QPushButton( a_pParent )
{
	m_bIsMouseOver = false;
	
	m_pDefaultBGImage = NULL;
	m_pOverBGImage = NULL;
	m_pClickBGImage = NULL;

	m_pDefaultFGImage = NULL;
	m_pOverFGImage = NULL;
	m_pClickFGImage = NULL;

	m_pFGRectangle = NULL;
	m_pFontRectange = NULL;

	m_nFontSize = 7;
	m_eTextAlignment = Qt::AlignHCenter;

	m_bHasEffect = false;
	m_pAnimation = new mkAnimation( this );
}

mkButton::~mkButton()
{
	MK_DELETE( m_pDefaultBGImage );
	MK_DELETE( m_pOverBGImage );
	MK_DELETE( m_pClickBGImage );

	MK_DELETE( m_pDefaultFGImage );
	MK_DELETE( m_pOverFGImage );
	MK_DELETE( m_pClickFGImage );

	MK_DELETE( m_pFGRectangle );

	MK_DELETE( m_pAnimation );
}

void mkButton::SetBGImage( IN QString a_strDefaultImage, IN QString a_strOverImage, IN QString a_strClickImage )
{
	MK_DELETE( m_pDefaultBGImage );
	MK_DELETE( m_pOverBGImage );
	MK_DELETE( m_pClickBGImage );
	
	m_pDefaultBGImage = new QImage( a_strDefaultImage );
	m_pOverBGImage = new QImage( a_strOverImage );
	m_pClickBGImage = new QImage( a_strClickImage );
}

void mkButton::SetFGImage( IN QString a_strDefaultImage, IN QString a_strOverImage, IN QString a_strClickImage, IN QRect *a_pRectangle )
{
	MK_DELETE( m_pDefaultFGImage );
	MK_DELETE( m_pOverFGImage );
	MK_DELETE( m_pClickFGImage );
	
	m_pDefaultFGImage = new QImage( a_strDefaultImage );
	m_pOverFGImage = new QImage( a_strOverImage );
	m_pClickFGImage = new QImage( a_strClickImage );

	if ( a_pRectangle ) {
		MK_DELETE( m_pFGRectangle );
		
		m_pFGRectangle = new QRect;
		memcpy( m_pFGRectangle, a_pRectangle, sizeof( QRect ) );	
	}
}

void mkButton::SetBGImage( IN QImage &a_rDefaultImage, IN QImage &a_rOverImage, IN QImage &a_rClickImage )
{
	MK_DELETE( m_pDefaultBGImage );
	MK_DELETE( m_pOverBGImage );
	MK_DELETE( m_pClickBGImage );

	m_pDefaultBGImage = new QImage( a_rDefaultImage );
	m_pOverBGImage = new QImage( a_rOverImage );
	m_pClickBGImage = new QImage( a_rClickImage );
}

void mkButton::SetFGImage( IN QImage &a_rDefaultImage, IN QImage &a_rOverImage, IN QImage &a_rClickImage, IN QRect *a_pRectangle )
{
	MK_DELETE( m_pDefaultFGImage );
	MK_DELETE( m_pOverFGImage );
	MK_DELETE( m_pClickFGImage );

	m_pDefaultFGImage = new QImage( a_rDefaultImage );
	m_pOverFGImage = new QImage( a_rOverImage );
	m_pClickFGImage = new QImage( a_rClickImage );

	if ( a_pRectangle ) {
		MK_DELETE( m_pFGRectangle );
		
		m_pFGRectangle = new QRect;
		memcpy( m_pFGRectangle, a_pRectangle, sizeof( QRect ) );	
	}
}

void mkButton::SetText( IN QString a_strText, QRect *a_pRectangle, int a_nFontSize, Qt::AlignmentFlag a_eAlignment )
{
	m_strText = a_strText;
	
	if ( a_pRectangle ) {
		MK_DELETE( m_pFontRectange );
		
		m_pFontRectange = new QRect;
		memcpy( m_pFontRectange, a_pRectangle, sizeof( QRect ) );	
	}

	m_nFontSize = a_nFontSize;
	m_eTextAlignment = a_eAlignment;
}

QString mkButton::GetText()
{
	return m_strText;
}

void mkButton::SetAnimation( IN bool a_bAnimation  )
{
	m_bHasEffect = a_bAnimation;
}

void mkButton::SetAnimationTick( IN int a_nMilliSec )
{
	m_pAnimation->SetAnimationTick( a_nMilliSec );
}

void mkButton::SetMaximumAnimationSize( IN QSize *a_pMaxSize )
{
	m_pAnimation->SetMaximumAnimationSize( a_pMaxSize );
}

void mkButton::paintEvent( IN QPaintEvent* a_pEvent )
{
	QPainter painter( this );
	
	QImage *pBGImage = m_pDefaultBGImage;
	QImage *pFGImage = m_pDefaultFGImage;
	QRect *pFGRectangle = m_pFGRectangle;

	if ( isDown() ) {
		pBGImage = m_pClickBGImage;
        pFGImage = m_pClickFGImage;
	} else if ( m_bIsMouseOver ) {
		pBGImage = m_pOverBGImage;
		pFGImage = m_pOverFGImage;

		if ( m_pAnimation->IsActivate() )
			pFGRectangle = m_pAnimation->GetAnimationImageRect();
	}

	if ( isChecked() ) {
		pBGImage = m_pClickBGImage;
		pFGImage = m_pClickFGImage;
	}

	/* Draw background image */
    if ( pBGImage && !pBGImage->isNull() )
		painter.drawImage( 0, 0, *pBGImage );

    /* Draw foreground image */
    if ( pFGImage && !pFGImage->isNull()) {
         if ( pFGRectangle ) {
        	 painter.drawImage( *pFGRectangle, *pFGImage );
         } else {
			painter.drawImage( 0, 0, *pFGImage );
         }
	}

	if ( !m_strText.isEmpty() ) {
		QFont trebuchetFont( QFont( MONKEY_FONT, m_nFontSize, m_nFontSize ));
		trebuchetFont.setCapitalization( QFont::Capitalize );
		painter.setFont( trebuchetFont );
		painter.setRenderHint( QPainter::TextAntialiasing );

		if ( isDown() || isChecked() ) painter.setPen( Qt::black );
	        else painter.setPen( Qt::white );

		QFontMetrics fontMetrics( trebuchetFont );
		if ( m_pFontRectange )
			painter.drawText( *m_pFontRectange, m_eTextAlignment,
								fontMetrics.elidedText( m_strText, Qt::ElideRight, width () ));
	}

	OnPaint( &painter );
}

void mkButton::enterEvent( IN QEvent* a_pEvent )
{
	m_bIsMouseOver = true;

	OnEnter( a_pEvent );
	
	if ( m_bHasEffect && m_pOverFGImage && m_pFGRectangle )
		m_pAnimation->StartAnimationEffect( m_pOverFGImage, m_pFGRectangle, eResizeEffect );

	update();
}

void mkButton::leaveEvent( IN QEvent* a_pEvent )
{
	m_bIsMouseOver = false;

	OnLeave( a_pEvent );

	update();
}
